﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Spotshub.Plugin.Payments.GlobalPayments.Services
{
    public partial class ServiceResult
    {
        public bool Success { get; set; }
        public List<string> Errors { get; set; }
        public string Code { get; set; }
        public string Message { get; set; }
        public string ResponseString { get; set; }
        public NameValueCollection NameValues { get; set; }

        public static string CodeName = "code";
        public static string MessageName = "message";

        public ServiceResult()
        {
            Errors = new List<string>();
        }

        public ServiceResult(string response)
        {
            Errors = new List<string>();

            try
            {
                ResponseString = response;
                NameValues = HttpUtility.ParseQueryString(response);
                Code = GetValue(CodeName);
                Message = GetValue(MessageName);
                int c;
                Success = int.TryParse(Code, out c) && c == 0;
            }
            catch (Exception e)
            {
                Success = false;
                Errors.Add(String.Format("Fatal error: {0}", e.ToString()));
            }
        }

        public string GetValue(string name)
        {
            if (NameValues != null)
            {
                return NameValues[name];
            }
            return null;
        }
    }

    public partial class CardOnFileUrlServiceResult : ServiceResult
    {
        public string URL { get; set; }
        public string UID { get; set; }
    }

    public partial class CardOnFileServiceResult : ServiceResult
    {
        public CardOnFileServiceResult(string response)
            : base(response)
        {

        }
        public string Token { get; set; }
        public string PNRef { get; set; }
        public string ApprovalCode { get; set; }
        public int CreditCardId { get; set; }
        public string Number { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }   
        public int ExpiredYear { get; set; }
        public int ExpiredMonth { get; set; }
    }

    public partial class RepeatSaleServiceResult : ServiceResult
    {
        public RepeatSaleServiceResult(string response)
            : base(response)
        {

        }
        public string TransactionId { get; set; }
        public string ApprovalCode { get; set; }
    }
}
